﻿using UnityEngine;
using System.Collections;

public class GameGUI : MonoBehaviour {
    public int leftTime;
    private float screenWidth;
    private float screenHeight;

	// Use this for initialization
	void Start () {
        InvokeRepeating ("CountTime", 1, 1);
        screenWidth = Screen.width;
        screenHeight = Screen.height;
        MapManager.gameState = MapManager.STATE_RUN;
	}
	
	// Update is called once per frame
	void Update () {
        Debug.Log("game update");
        if (Time.deltaTime > 0) 
        {
            if (0 == leftTime) 
            {
                MapManager.gameState = MapManager.STATE_END;
            }
        }
        
    }
    
    void OnGUI() {
        Debug.Log("game onGui");
        if (GUI.Button (new Rect (10, 10, 80, 50), "score\n" + Utils.score.ToString ())) 
        {
            print ("You clicked the button!");
        }
        if (GUI.Button (new Rect (100, 10, 80, 50), "time\n" + leftTime)) 
        {
            if(MapManager.STATE_RUN == MapManager.gameState)
            {
                Time.timeScale = 0;
                MapManager.gameState = MapManager.STATE_PAUSE;
            }
            else if(MapManager.STATE_PAUSE == MapManager.gameState)
            {
                Time.timeScale = 1;
                MapManager.gameState = MapManager.STATE_RUN;
            }
        }
        if (MapManager.STATE_END == MapManager.gameState) 
        {
            Time.timeScale = 0;
            if (GUI.Button (new Rect (screenWidth / 2 - 100, screenHeight / 2 - 50, 200, 100), "Game End !\nYou got" + Utils.score + "points\n click to try agian" )) 
            {

                Time.timeScale = 1;
                Utils.score = 0;
                Application.LoadLevel(Application.loadedLevelName);
                MapManager.gameState = MapManager.STATE_RUN;
            }
        }
    }

    void CountTime()
    {
        if (leftTime > 0) {
            leftTime --;
        } 
        else
        {
            CancelInvoke("CountTime");
        }
    }
}
